package com.medical.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.medical.entity.TbArea;
import com.medical.service.AreaService;

@Controller
@RequestMapping("/area")
public class AreaController {

	@Autowired
	private AreaService areaService;
	
	/**
	 * 分页获取区域信息列表
	 * @param currentPage
	 * @param limit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getAreaList")
	public String getAreaList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
		String areaName = request.getParameter("context");
		currentPage = currentPage != null ? currentPage : 1;
		limit = limit != null ? limit : 10;
		
		PageInfo<TbArea> pageInfo = areaService.getAllArea(areaName, currentPage, limit);
		
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("context", areaName);
		
		return "/area/area_list";
	}
	
	@RequestMapping("/openAdd")
	public String openAdd(Model model) {
		return "/area/area_add";
	}
	
	@RequestMapping("/openEdit")
	public String openEidt(Integer id, Model model) {
		TbArea area = areaService.getAreaById(id);
		
		model.addAttribute("area", area);
		return "/area/area_edit";
	}
	
	@RequestMapping("/add")
	public String add(TbArea area, HttpServletRequest request, Model model) {
		areaService.add(area);
		return getAreaList(1, 10, request, model);
	}
	
	@RequestMapping("/edit")
	public String edit(TbArea area, HttpServletRequest request, Model model) {
		areaService.edit(area);
		return getAreaList(1, 10, request, model);
	}
	
	@RequestMapping("/delete")
	public String delete(Integer id, HttpServletRequest request, Model model) {
		areaService.delete(id);
		return getAreaList(1, 10, request, model);
	}
	
	@ResponseBody
	@RequestMapping("getAreaTreeList")
	public Object getAreaTreeList() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("areaList", areaService.getAreaTree(""));
		return map;
	}
	
}
